from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{设函数}$\int _{0}^{x}f\left( t\right) dt$\text{在}$\left[ 0,1\right] $\text{上二阶导数连续,}$\int _{0}^{1}f\left( x\right) dx=1,\int _{0}^{\frac{1}{2}}f\left( x\right)  >\dfrac{1}{4}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
        text1 = Tex(
                   r"\text{(1)存在}$\xi \in \left( 0,1\right) ,$\text{使得}$f'\left( \xi \right)  <2$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-5,0])
        text2 = Tex(
                    r"\text{(2)若当}$0 <x <1$\text{时,}$f'\left( x\right) \neq 2,$\text{则}$\int _{0}^{x}f\left( t\right) dt >x^{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-11,-10,0])
        text3 = Tex(
                   r"\text{,证明：}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text,RIGHT,buff=1)

        self.play(Write(text),self.camera.frame.animate.scale(0.8),Write(text1),Write(text2),Write(text3))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,20,0]),text1.animate().move_to([-20,15,0]) ,text2.animate().move_to([-11,10,0]),text3.animate().move_to([44,20,0]) )

        #箭头引出+文本
        arrow = Arrow(start=[10,6,0],end=[10,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))

        text6 = Tex(r"$\int _{0}^{x}f\left( t\right) dt-x^{2} >0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10, -5, 0])
        self.play(Create(text6))
        text7 = Tex(r"\text{即证}$H\left( x\right)  >0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10, -10, 0])
        self.play(Create(text7))


        #画坐标轴

        axes = Axes(
            x_range=[-PI, 2*PI, PI],  # 设置 x 轴范围，从 -3π 到 3π，步长为 π/2
            y_range=[-1.5, 1.5, 0.5],     # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE,"stroke_width": 30},  # 设置坐标轴的颜色
            x_length=40,  # 控制 x 轴长度
            y_length=20 ,   # 控制 y 轴长度
        )
        axes.move_to([-25,-10,0])
        # 绘制 cos(x) 函数图像，负半轴部分为蓝色
        sin_graph_left = axes.plot(lambda x: np.sin(x), color=BLUE,stroke_width=30, x_range=[0, PI])

        # 添加坐标系和函数图像到场景中
        #self.add(axes, cos_graph_left, cos_graph_right)

        # 添加标题
        title = Text("H(x)",color=GREEN, font_size=240).next_to(axes, UP,buff=1)
        self.add(title)

        # 显示图像
        self.play(Create(axes),  Write(title))
        
        #三个点
        dot4 = Dot(radius=0.5, color=RED)  
        dot4.move_to([-31.28,-10,0]) 
        dot5 = Dot(radius=0.5, color=YELLOW)  
        dot5.move_to([-19.28,-10,0])  
        dot6 = Dot(radius=0.5, color=GREEN)  
        dot6.move_to([-25,-4.28,0])
        self.play(Create(dot4), Create(dot5),Create(dot6) )


        self.play( Create(sin_graph_left))


        #文本

        text8 = Tex(r"\text{即证}$H^{''}\left( x\right)  <0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20, -15, 0])
        self.play(Create(text8))

        #半透明矩阵扩展
        rectangle=Rectangle(color=BLUE,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-9.5,10,0])
        self.play(
            rectangle.animate.stretch(11.0,dim=0)
        )

        text9 = Tex(r"$H^{''}\left( x\right) \neq 0$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10, 5, 0])
        self.play(Create(text9))

        #箭头消失
        self.play(FadeOut(arrow))
        #文本移动
        self.play(text6.animate().move_to([35,5,0]),text7.animate().move_to([35,0,0])) 

        #箭头引出+文本
        arrow1 = Arrow(start=[-10,2,0],end=[-3,-2,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow1))

        text10 = Tex(r"$H^{''}\left( x\right)  >0$\text{或}$H^{''}\left( x\right)  <0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10, -5, 0])
        self.play(Create(text10))

        # #半透明矩阵扩展
        rectangle1=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-6.5,15,0])
        self.play(
            rectangle1.animate.stretch(11.0,dim=0)
        )

        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=11.5,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([2.5, -5, 0])
        self.play(Create(rectangle2))
        self.wait(0.1)
        #框框移动
        self.play(rectangle2.animate().move_to([17.5, -5, 0]))







        self.wait()